package by.semerikov.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import by.semerikov.constants.ProposalStates;
import by.semerikov.constants.Results;
import by.semerikov.exceptions.ConnectorException;
import by.semerikov.exceptions.DAOException;
import by.semerikov.form.CreateTripForm;
import by.semerikov.model.Person;
import by.semerikov.model.Proposal;
import by.semerikov.service.ProposalService;
import by.semerikov.service.ServiceFactory;

public class CreateTrip extends SecuredAction {

	@Override
	public ActionForward executedInternal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, Person user) throws DAOException, ConnectorException {
		CreateTripForm createTripForm = (CreateTripForm) form;
		Proposal newProposal = new Proposal(null, createTripForm.getName(),
				createTripForm.getDescription(), createTripForm.getFrom(),
				createTripForm.getTo(), createTripForm.getCost(),
				ProposalStates.FREE, null);
		ProposalService proposalService = ServiceFactory.getInstance()
				.getProposalService();
			proposalService.createProposal(newProposal);
			ActionMessages errors = new ActionMessages();
			errors.add("message", new ActionMessage("message.saved"));
			saveErrors(request, errors);
		return mapping.findForward(Results.SUCCESS + user.getPost());
	}
}
